package com.te;

import org.jbox2d.common.Vec2;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener {

    PhysicsWorld                mWorld;
    private Handler             mHandler;
    private final static String TAG  = MainActivity.class.getSimpleName();
    SensorManager               sensorManager;
    Sensor                      accSensor, magSensor;
    float[]                     mAcc = null;
    float[]                     mMag = null;
    GameWorld               elH  = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        
        
        
      
        
        elH = new GameWorld(this,displayMetrics.heightPixels,displayMetrics.widthPixels);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        TextView v = new TextView(this);
        v.setText("whta");
        setContentView(elH);

        Log.d(TAG, "onCreate");
        
    
       
        /*
         * mWorld = new PhysicsWorld(); mWorld.create();
         * 
         * // Add 50 Balls for (int i=0; i<50; i++) { mWorld.addBall(); }
         * 
         * // Start Regular Update mHandler = new Handler(); mHandler.post(update);
         */

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

    }

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this, magSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    private Runnable update = new Runnable() {
                                public void run() {
                                    mWorld.update();
                                    mHandler.postDelayed(update, (long) (mWorld.timeStep * 1000));
                                }
                            };

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Log.d(TAG, "onAccuracyChanged " + accuracy);

    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    public void onSensorChanged(SensorEvent event) {

        Sensor sensor = event.sensor;
        switch (sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
                // Log.d(TAG, "acc");
                mAcc = event.values.clone();
                break;
            case Sensor.TYPE_MAGNETIC_FIELD:
                // Log.d(TAG, "mag");
                mMag = event.values.clone();
                break;
        }
        if (mAcc == null || mMag == null) {
            return;
        }
        // Log.d(TAG, "or " + mAcc[0] + "," + mAcc[1] + "," + mAcc[2]);

        Vec2 gravity = new Vec2();
        gravity.x = -mAcc[0];
        gravity.y = mAcc[1];
        if (elH.world!=null) {
            elH.world.setGravity(gravity);
        }
        /*
         * float R[] = new float[9]; float mOrientation[] = new float[3]; if (SensorManager.getRotationMatrix(R, null, mAcc,
         * mMag)) {
         * 
         * SensorManager.getOrientation(R, mOrientation); Log.d(TAG, "or " + mOrientation[0] + "," + mOrientation[1] + "," +
         * mOrientation[2]); }
         */
    }

}
